<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="addbtn">

          <el-button class="filter-item fr" size="small" style="margin-left: 10px;" @click="handleCreate" type="success" icon="el-icon-edit">{{$t('table.addQuestion')}}</el-button>
        </div>
        <!-- 精选题库搜索 -->
        <el-form class="demo-form-inline" label-position="right" label-width="70px" size="small" :model="ruleForm">
          <el-row :gutter="5">
            <el-col :span="6">
              <el-form-item label="学科 ">
                <el-select v-model="ruleForm.subjectID" placeholder="请选择">
                  <el-option label="请选择" value=""></el-option>
                  <el-option v-for="item in subjectlist" :label="item.label" :key="item.value" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input v-model="ruleForm.keyword" placeholder="根据题干搜索"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button @click="resetForm">清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!-- 列表展示 -->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane v-for="item in chkType" :label="item.label" :name="item.value" :key="item.value">
            <el-alert :title="`数据一共${total}条`" type="info" show-icon></el-alert>
            <el-table :data="tableData" border style="width: 100%">
              <el-table-column prop="number" label="试题编号" width="150"></el-table-column>
              <el-table-column prop="subject" label="学科" width="120"></el-table-column>
              <el-table-column prop="catalog" label="目录" width="120"></el-table-column>
              <el-table-column label="题型" width="120">
                <template v-slot="{row}">
                  {{getQuestionType(row.questionType)}}
                </template>
              </el-table-column>
              <el-table-column label="题干" width="300">
                <template v-slot="{row}">
                  <p v-html="row.question"></p>
                </template>
              </el-table-column>
              <el-table-column prop="addDate" label="录入时间" width="120">
                <template v-slot="{row}">
                  {{row.addDate | formdata}}
                </template>
              </el-table-column>
              <el-table-column label="难度" width="120">
                <template v-slot="{row}">
                  {{getDifficulty(row.difficulty)}}
                </template>
              </el-table-column>
              <el-table-column prop="creator" label="录入人" width="120"></el-table-column>
              <el-table-column prop="chkState" label="审核状态" width="120">
                <template v-slot="{row}">
                  {{getChkState(row.chkState)}}
                </template>
              </el-table-column>
              <el-table-column prop="chkUser" label="审核人" width="120"></el-table-column>
              <el-table-column prop="chkRemarks" label="审核意见" width="120"></el-table-column>
              <el-table-column label="发布状态" width="120">
                <template v-slot="{row}">
                  {{getPublishType(row.publishState)}}
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                  <el-button @click="handleView(scope.row.id)" type="text" size="small">预览</el-button>
                  <el-button type="text" size="small" @click="choiceCheck(scope.row)" :disabled="scope.row.chkState==0?false:true">审核</el-button>
                  <el-button>修改</el-button>
                  <el-button type="text" size="small" @click="choicePublishOrUn(scope.row)">{{scope.row.publishState==1?'下架':'上架'}}</el-button>
                  <el-button type="text" size="small" @click="delQuestion(scope.row.id)" :disabled="scope.row.publishState==1?true:false">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
        <!-- 分页 -->
        <el-pagination background :total="total" :page-sizes="[2,5,10, 20, 50]" :page-size="5" layout="sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange">
        </el-pagination>
        <!---审核弹框-->
        <QuestionCheck :dialogFormVisible.sync="dialogFormVisible" :id="id" @submitHandle="submitHandle" />
        <!-- 题目预览 -->
        <QuestionChoicePreview :previewdialog.sync="previewdialog" :previewId="previewId" />
      </el-card>
    </div>
  </div>
</template>

<script>
// choiceCheck
import { choice, choicePublish, remove } from '@/api/hmmm/questions.js'
import { simple } from '@/api/hmmm/subjects.js'
import { questionType, difficulty, publishType } from '@/api/hmmm/constants.js'
import Moment from 'moment'
import QuestionCheck from '../components/questions-check.vue'
import QuestionChoicePreview from '../components/question-choice-preview.vue'
export default {
  components: { QuestionCheck, QuestionChoicePreview },
  filters: {
    formdata (date) {
      return Moment(date).format('yy-MM-DD HH-mm-ss')
    }
  },
  data () {
    return {
      previewId: 0,
      previewdialog: false,
      id: 0,
      dialogFormVisible: false,
      page: '',
      pagesize: 5,
      ruleForm: {
        subjectID: '',
        keyword: ''
      },
      chkState: '999',
      subjectlist: [], // 学科列表
      activeName: '999',
      chkType: [
        {
          value: '999',
          label: '全部'
        },
        {
          value: '0',
          label: '待审核'
        },
        {
          value: '1',
          label: '通过'
        },
        {
          value: '2',
          label: '拒绝'
        }
      ],
      tableData: [],
      total: 0,
      directorysList: []
    }
  },
  created () {
    this.getSubjectList()
    this.getQuestionChoiceList()
  },
  methods: {
    // 题目预览
    handleView (id) {
      this.previewdialog = true
      this.previewId = id
    },
    // 删除精选
    async delQuestion (id) {
      const result = await this.$confirm('确定要删除此条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {

      })
      if (result !== 'confirm') return
      try {
        const res = await remove({ id })
        console.log(res)
        if (res.status === 200) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.getQuestionChoiceList()
        }
      } catch {

      }
    },
    // 审核
    choiceCheck (row) {
      this.dialogFormVisible = true
      this.id = row.id
    },
    submitHandle () {
      this.dialogFormVisible = false
      this.getQuestionChoiceList()
    },
    // 题上下架
    async choicePublishOrUn (row) {
      const publishText = row.publishState === 1 ? '下架' : '上架'
      const result = await this.$confirm(`确定要${publishText}吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {

      })
      if (result !== 'confirm') return
      const publishState = row.publishState === 1 ? 0 : 1
      const res = await choicePublish({ publishState, id: row.id })
      if (res.status === 200) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.getQuestionChoiceList()
      } else {
        this.$message.error('删除失败')
      }
    },
    resetForm () {
      this.ruleForm = this.$options.data().ruleForm
    },
    getDifficulty (v) {
      v = Number(v)
      const questType = difficulty.find(item => item.value === v)
      return questType ? questType.label : ''
    },
    getQuestionType (v) {
      v = Number(v)
      const questType = questionType.find(item => item.value === v)
      return questType ? questType.label : ''
    },
    // 发布状态
    getPublishType (v) {
      const request = publishType.find(item => parseInt(item.value) === v)
      return request ? request.label : ''
    },
    // 审核状态
    getChkState (v) {
      const request = this.chkType.find(item => parseInt(item.value) === v)
      return request ? request.label : ''
    },
    // 加载学科列表
    async getSubjectList () {
      const res = await simple()
      this.subjectlist = res.data
    },
    // 精选题库列表
    async getQuestionChoiceList () {
      const search = {}
      if (this.ruleForm.subjectID) {
        search.subjectID = this.ruleForm.subjectID
      }
      if (this.ruleForm.keyword) {
        search.keyword = this.ruleForm.keyword
      }
      if (this.chkState < 10) {
        search.chkState = parseInt(this.chkState)
      }
      console.log(search, this.chkState)
      const res = await choice({ ...search, page: this.page, pagesize: this.pagesize })
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    // 分页
    handleCurrentChange (val) {
      this.page = val
      this.getQuestionChoiceList()
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.getQuestionChoiceList()
    },
    // 新增题库
    handleCreate () {
      this.$router.push('/questions/new')
    },
    // 按条件搜索
    onSubmit () {
      this.getQuestionChoiceList()
    },
    handleClick (tab, event) {
      this.chkState = Number(tab.name)
      this.getQuestionChoiceList()
    }
  }
}
</script>

<style scoped lang='scss'>
.addbtn {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}
.el-table {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>
